package com.bamanker.cloudconsumer.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.bamanker.cloudapi.model.error.CommonErrorCode;
import com.bamanker.cloudapi.model.exception.BusinessException;
import com.bamanker.cloudconsumer.feign.ResClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author bamanker
 */
@RestController
@Slf4j
@RequiredArgsConstructor
public class ResourceController {

    private final ResClient resClient;

    /**
     * 使用feign调用
     */
    @GetMapping("/a/resource1")
    public Object getRes1() {
        return resClient.getRes1();
    }

    @GetMapping("/a/resource2")
    public Object getRes2() {
        return resClient.getRes2();
    }

    @GetMapping("/a/resource3")
    public Object geRes3() {
        return resClient.getRes3();
    }

    @GetMapping("/api/a/publicResource")
    @SentinelResource(value = "getResPublic",
            blockHandler = "getResPublicBlock")
    public Object getResPublic() {
        return resClient.getPublicRes();
    }

    public Object getResPublicBlock(BlockException e) {
        log.error("访问资源 getResPublic 被限流，参数：");
        throw new BusinessException(CommonErrorCode.TOO_MANY_REQUEST, "访问资源 getResPublic 被限流",e.getCause());
    }

    @GetMapping("/a/test01")
    public Object getResTest01() {
        return resClient.getResTest01();
    }

    @GetMapping("/a/test02")
    public Object getResTest02() {
        return resClient.getResTest02();
    }

    @GetMapping("/a/app")
    public Object getResApp() {
        return resClient.getResApp();
    }

    @GetMapping("/a/web")
    public Object getResWeb() {
        return resClient.getResWeb();
    }

    @GetMapping("/a/projects")
    public Object getResProjects() {
        return resClient.getResProjects();
    }

    @GetMapping("/a/userInfo")
    public Object getResUserInfo() {
        return resClient.getResUserInfo();
    }

}
